package vendalenger.debvi.jfx;

import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.util.ArrayList;

import javax.imageio.stream.FileImageInputStream;

import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TabPane.TabClosingPolicy;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import vendalenger.debvi.Debvi;
import vendalenger.skin.FxSkinner;

public class DebviFrame {

	public static ArrayList<String> tabTags;
	public static Graphics mainCanvasGraphics;
	public static TabPane tabContainer;
	public static VD_FileTreeView browser;
	
	public static void addTab(Tab t, String tag) {
		tabContainer.getTabs().add(t);
		tabTags.add(tag);
	}
	
	/**
	 * Don't use this.
	 * 
	 * @param mainStage The stage from the start method in a JavaFX Application.
	 */
	public static void setup(Stage mainStage) {
		StackPane rootPane = new StackPane(); /* Make the rootpane a stack pane so other nodes could overlap the main pane*/
		GraphicsDevice sgd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
		Scene scene = new Scene(rootPane, sgd.getDisplayMode().getWidth() / 1.33, sgd.getDisplayMode().getHeight()/ 1.33);
		try {
			browser = new VD_FileTreeView(new File("."), new ImageView(new Image(new File("Resource/Icons/TreeView/project.png").toURI().toURL().toString()))); /* Create the file browser on the left */
		} catch (MalformedURLException e1) {
			e1.printStackTrace();
		}
		
		FxSkinner.setup(scene); /* Setup the skin. also known as the look and feel or theme */
		
		BorderPane mainPane = new BorderPane(); /* Create the main pane */
		
		browser.setPadding(new Insets(8, 4, 8, 8));
		browser.setOnOpen(new EventHandler<MouseEvent>() { /* make the browser open a the file on double click */
			@Override
			public void handle(MouseEvent event) {
				if (event.getClickCount() >= 2 && event.getButton() == MouseButton.PRIMARY) {
					System.out.println("Open! Eggs");
					browser.getSelectedFile();
				}
				if (event.getButton() == MouseButton.SECONDARY) {
					System.out.println("Right Click!");
				}
			}
		});
		
		tabContainer = new TabPane();
		tabContainer.setPadding(new Insets(8, 8, 8, 4));
		tabContainer.setTabClosingPolicy(TabClosingPolicy.ALL_TABS);
		
		mainPane.setLeft(browser);
		mainPane.setCenter(tabContainer);
		rootPane.getChildren().add(mainPane);
		
		mainStage.getIcons().add(new Image("file:Resource/Icons/Window/debvi32.png"));
		mainStage.getIcons().add(new Image("file:Resource/Icons/Window/debvi48.png"));
		mainStage.getIcons().add(new Image("file:Resource/Icons/Window/debvi64.png"));
		
		mainStage.setTitle("Debvi - " + Debvi.getDebviVersion());
		mainStage.setScene(scene);
		mainStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
			@Override public void handle(WindowEvent event) {
				System.out.println("Debvi is closing...");
				System.exit(0);
			}
		});
		mainStage.show();
	}
}
